package com.wn.ticket.web.backstage;

import com.wn.ticket.common.ResponseEntity;
import com.wn.ticket.entity.BackstageMenu;
import com.wn.ticket.mapper.BackstageMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wuqingting
 * @date 2022/9/1
 */
@RestController
@RequestMapping("/backstage/menu")
public class MenuController {
    @Autowired
    private BackstageMenuMapper backstageMenuMapper;

    @RequestMapping("/getAll")
    public ResponseEntity getAll(){
        List<BackstageMenu> menuList = backstageMenuMapper.getAll();
        List<BackstageMenu> backstageMenus = streamToTree1(menuList, 0);

        return new ResponseEntity(backstageMenus);
    }

    private List<BackstageMenu> streamToTree1(List<BackstageMenu> treeList, int parentId) {
        //利用递归将list转换为树状
        List<BackstageMenu> list = treeList.stream()
                // 过滤父节点
                .filter(parent -> parent.getParentId() == parentId)
                // 把父节点children递归赋值成为子节点
                .map(child -> {
                    child.setChildren(streamToTree1(treeList, child.getId()));
                    return child;
                }).collect(Collectors.toList());//将流中所有元素导入一个列表
        return list;
    }

}
